﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using BaiTu.EventServices.Configuration;
using BaiTu.IEventServices;
using BaiTu.EventServices.Providers;
using System.Threading;
using BaiTu.IOCServices;
using System.Reflection;
using System.Diagnostics;
using System.Text.RegularExpressions;
using BaiTu.Framework.Core.TypeFinder;

namespace BaiTu.EventServices.Dependency
{
    public class Dependency : IOCServices.IDependencyRegistrater
    {

        public void Register(ContainerBuilder builder, Framework.Core.TypeFinder.ITypeFinder typeFinder)
        {
            IEventBusProvider eventBusProvider = null;
            //IoC注入
            switch (EventServiceConfiguration.EventBusType)
            {
                case EventBusType.Local:
                    //ObjectFactory.RegisterAsSingleton<IEventBusProvider, LocalEventBusProvider>();
                    builder.RegisterType<LocalEventBusProvider>().As<IEventBusProvider>();
                    eventBusProvider = new LocalEventBusProvider();
                    break;
                case EventBusType.Redis:
                    builder.RegisterType<RedisEventBusProvider>().As<IEventBusProvider>();
                    eventBusProvider = new RedisEventBusProvider();
                    //ObjectFactory.RegisterAsSingleton<IEventBusProvider, RedisEventBusProvider>();
                    break;
                case EventBusType.SuperSocket:
                    builder.RegisterType<SuperSocketEventBusProvider>().As<IEventBusProvider>();
                    //eventBusProvider = new SuperSocketEventBusProvider();
                    //ObjectFactory.RegisterAsSingleton<IEventBusProvider, SuperSocketEventBusProvider>();
                    break;
            }
            builder.RegisterType<EventBusService>().As<IEventBus>().SingleInstance();
            //ObjectFactory.RegisterAsSingleton<IEventBus, EventBusService>();
            RegisterEvents(eventBusProvider);
            EventQueueProcessor qp = new EventQueueProcessor();
            ThreadPool.QueueUserWorkItem(new WaitCallback(qp.Process));

            //Thread.Sleep(200);

           
        }
        internal static void RegisterEvents(IEventBusProvider busProvider)
        {
            ///默认系统订阅的事件
            //foreach (var map in EventServiceConfiguration.EventMappings)
            //{
            //    busProvider.Subscribe(map.SourceClassType);
            //}

            //var typeFinder = EngineContext.Current.Resolve<ITypeFinder>();
            //List<Type> listEventProcessType = typeFinder.FindClassesOfType<Framework.Core.Interfaces.IProcessReceivedEventProcessorHandler>().ToList();
            //if (listEventProcessType != null && listEventProcessType.Count > 0)
            //{
            //    foreach (var item in listEventProcessType)
            //    {
            //        EventEntity.EventInfo eventInfo = new EventEntity.EventInfo()
            //        {
            //             ProcessAssemblyName=item.AssemblyQualifiedName,
            //              ProcessClassType=item.FullName,
            //               SourceAssemblyName=
            //        };
            //    }
                
            //    //EventServiceConfiguration.EventMappings.Add();
            //}
        }

        public int Order
        {
            get { return 300; }
        }


    }

}
